<?php

namespace app\dao;

use app\model\HonorModel;
use think\facade\Log;
use think\Paginator;
class HonorDao extends BaseDao
{
    private $query;

    // 设置模型
    protected function setModel()
    {

        return HonorModel::class;
    }
    public function __construct()
    {
        // 创建一个HonorModel对象，并将其赋值给$this->query属性
        $this->query = new HonorModel();
    }
    // 创建荣誉记录
    public function create(array $data)
    {
        try {
            return $this->getModel()->create($data);
        } catch (\Exception $e) {
            Log::error("创建荣誉记录失败: " . $e->getMessage());
            return null;
        }
    }

    // 更新荣誉记录
    public function update($id, array $data)
    {
        try {
            return $this->query->where('id', $id)->update($data);
        } catch (\Exception $e) {
            Log::error("更新荣誉记录失败: " . $e->getMessage());
            return null;
        }
    }

    // 根据 ID 获取荣誉记录
    public function getHonorById($id)
    {
        try {
            return $this->query->where('id', $id)->find();
        } catch (\Exception $e) {
            Log::error("根据ID获取荣誉记录失败: " . $e->getMessage());
            return null;
        }
    }

    // 获取所有荣誉记录
    public function getAllHonors()
    {
        try {
            return $this->query->select();
        } catch (\Exception $e) {
            Log::error("获取所有荣誉记录失败: " . $e->getMessage());
            return [];
        }
    }

    // 审核荣誉记录
    public function verifyHonor($id, $status, $userId)
    {
        try {
            $honor = $this->getHonorById($id);
            if ($honor) {
                $honor->status = $status;
                $honor->verified_by = $userId;
                $honor->verified_at = now();
                return $honor->save();
            }
            return false;
        } catch (\Exception $e) {
            Log::error("审核荣誉记录失败: " . $e->getMessage());
            return false;
        }
    }
}